<template>
  <el-dialog title="选择合同条款"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
        <el-form-item label="公司名称" prop="factoryName">
          <el-input
            v-model="queryParams.factoryName"
            placeholder="请输入公司名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="合同版本编号" prop="contractVersionNumber">
          <el-input
            v-model="queryParams.contractVersionNumber"
            placeholder="请输入合同版本编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="NcContractTermsList" @selection-change="handleSelectionChange" @current-change="handleCurrent">
        <el-table-column  width="55" align="center" >
          <template v-slot="scope">
            <el-radio v-model="selectedItemId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
          </template>
        </el-table-column>
        <el-table-column label="公司名称" align="center" prop="factoryName" />
        <el-table-column label="公司代码" align="center" prop="factoryCode" />
        <el-table-column label="合同版本编号" align="center" prop="contractVersionNumber" />
        <el-table-column label="甲方" align="center" prop="partyA" />
        <el-table-column label="甲方法定代表人" align="center" prop="legalRepresentativeOfPartyA" />
        <el-table-column label="甲方委托代理人" align="center" prop="authorizedAgentOfPartyA" />
        <el-table-column label="甲方电话" align="center" prop="partyAPhone" />
        <el-table-column label="付款条件" align="center" prop="paymentTerms" />
        <el-table-column label="付款方式" align="center" prop="paymentMethod" />
        <el-table-column label="发票开具" align="center" prop="invoiceIssuance" />
        <el-table-column label="服务范围" align="center" prop="serviceScope" />
        <el-table-column label="服务要求" align="center" prop="serviceRequirements" />
        <el-table-column label="双方职责" align="center" prop="responsibilitiesOfBothParties" />
        <el-table-column label="甲方权利义务" align="center" prop="rightsAndObligationsOfPartyA" />
        <el-table-column label="乙方权利义务" align="center" prop="rightsAndObligationsOfPartyB" />
        <el-table-column label="质量要求" align="center" prop="qualityRequirements" />
        <el-table-column label="验收标准" align="center" prop="acceptanceCriteria" />
        <el-table-column label="质保期" align="center" prop="warrantyPeriod" />
        <el-table-column label="知识产权" align="center" prop="intellectualPropertyRights" />
        <el-table-column label="保密条款" align="center" prop="confidentialityClause" />
        <el-table-column label="违约责任" align="center" prop="breachOfContractLiability" />
        <el-table-column label="合同规则" align="center" prop="contractRules" />
        <el-table-column label="不可抗力" align="center" prop="forceMajeure" />
        <el-table-column label="其他" align="center" prop="other" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listNcContractTerms, getNcContractTerms, delNcContractTerms, addNcContractTerms, updateNcContractTerms } from "@/api/sales/NcContractTerms";
import {getToken} from "@/utils/auth";
export default {
  name: "NcContractTerms",
  data() {
    return {
      showFlag:false,
      selectedItemId: undefined,
      selectedRows: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同条款表格数据
      NcContractTermsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryName: null,
        contractVersionNumber: null,
      },
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "导入合同信息",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/sales/NcContractTerms/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    show() {
      this.showFlag = true;
      this.selectedRows={}
      this.selectedItemId=undefined
    },
    /** 查询合同条款列表 */
    getList() {
      this.loading = true;
      listNcContractTerms(this.queryParams).then(response => {
        this.NcContractTermsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryName: null,
        factoryCode: null,
        contractVersionNumber: null,
        partyA: null,
        legalRepresentativeOfPartyA: null,
        authorizedAgentOfPartyA: null,
        partyAPhone: null,
        paymentTerms: null,
        paymentMethod: null,
        invoiceIssuance: null,
        serviceScope: null,
        serviceRequirements: null,
        responsibilitiesOfBothParties: null,
        rightsAndObligationsOfPartyA: null,
        rightsAndObligationsOfPartyB: null,
        qualityRequirements: null,
        acceptanceCriteria: null,
        warrantyPeriod: null,
        intellectualPropertyRights: null,
        confidentialityClause: null,
        breachOfContractLiability: null,
        contractRules: null,
        forceMajeure: null,
        other: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同条款";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNcContractTerms(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同条款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNcContractTerms(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNcContractTerms(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据？').then(function() {
        return delNcContractTerms(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sales/NcContractTerms/export', {
        ...this.queryParams
      }, `NcContractTerms_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "合同条款导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('sales/NcContractTerms/importTemplate', {
      }, `合同条款模板.xlsx`)
    },
    /** 文件上传中处理 */
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    /** 文件上传成功处理 */
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    /** 提交上传文件 */
    submitFileForm() {
      this.$refs.upload.submit();
    },
    handleCurrent(row){
      if(row){
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    /** 提交按钮 */
    confirmSelect() {
      if(this.selectedItemId ==null || this.selectedItemId==0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRows);
      this.showFlag = false;
    },
  }
};
</script>
<style lang="scss" scoped>
  ::v-deep .cell .el-input__inner{
    border:none;
  }
</style>

